/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.utils.Const;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamType;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamTypeService;

/**
 * 模拟考试业务Controller
 * @author zms
 * @version 2018-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoexam/campusinfoExamType")
public class CampusinfoExamTypeController extends BaseController {

	@Autowired
	private CampusinfoExamTypeService campusinfoExamTypeService;
	
	@ModelAttribute
	public CampusinfoExamType get(@RequestParam(required=false) String id) {
		CampusinfoExamType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoExamTypeService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoExamType();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoExamType:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoExamType campusinfoExamType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoExamType> page = campusinfoExamTypeService.findPage(new Page<CampusinfoExamType>(request, response), campusinfoExamType); 
		model.addAttribute("page", page);
		return "modules/campusinfoexam/campusinfoExamTypeList";
	}

	@RequiresPermissions("campusinfoexam:campusinfoExamType:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoExamType campusinfoExamType, Model model) {
		model.addAttribute("campusinfoExamType", campusinfoExamType);
		return "modules/campusinfoexam/campusinfoExamTypeForm";
	}

	@RequiresPermissions("campusinfoexam:campusinfoExamType:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoExamType campusinfoExamType, Model model, HttpServletRequest request,RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoExamType)){
			return form(campusinfoExamType, model);
		}
		if(StringUtils.isNotBlank(campusinfoExamType.getExamImg())) {
			campusinfoExamType.setExamImg(Const.getinetAddressService(campusinfoExamType.getExamImg(),request));
		}
		campusinfoExamTypeService.save(campusinfoExamType);
		addMessage(redirectAttributes, "保存业务成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoExamType/?repage";
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoExamType:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoExamType campusinfoExamType, RedirectAttributes redirectAttributes) {
		campusinfoExamTypeService.delete(campusinfoExamType);
		addMessage(redirectAttributes, "删除业务成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoExamType/?repage";
	}

}